package top.ewind.controller.admin.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.redis.Redis;

import top.ewind.controller.common.AdminRootCtrl;
import top.ewind.controller.common.RedisConstants;
import top.ewind.util.DateUtil;
import top.ewind.util.SecurityUtil;
public class DeviceUserCtrl extends AdminRootCtrl {	
	
	//用户列表
	public void list(){
		render("list.html");
	}
	
	public void profile() {
		int nid = getSessionAttr("nid");
		String filter = SecurityUtil.sqlFilter(getPara("param", ""));
		renderJson(Db.paginate(getParaToInt("page",1), getParaToInt("rp",10), "select *","from device_user where nid = "+nid+ filter));
	}
	
	//用户绑定的设备列表
	public void userdevice(){
		setAttr("userid", getPara(0));
		int nid = getSessionAttr("nid");
		setAttr("plist",Db.find("select * from device_product where nid = ? order by intime desc",nid));
		render("userdevice.html");
	}

	public void userdeviceProfile() {
		String filter = SecurityUtil.sqlFilter(getPara("param", ""));
		renderJson(Db.paginate(getParaToInt("page",1), getParaToInt("rp",10), 
				"select d.stat,d.sn,t.id,t.udname,t.deviceid,t.bindtime,t.btype", " from device_bind t left join device d on d.id = t.deviceid where t.userid = ? " + filter , getPara(0)));
	}
	
	//用户解绑设备
	public void userdeviceDel() {
		boolean ret = Db.deleteById("device_bind",getPara("id","0"));
		if(ret)renderJson("msg","success");
		else renderJson("msg","删除失败，请重试！");
	}
	
	//用户禁用/启用
	public void chgstat(){
		boolean ret = false;
		Record user = Db.findById("device_user",getPara("id"));
		if(user!=null){
			user.set("stat", getPara("stat"));
			ret = Db.update("device_user", user);
		}
		if(ret){
			renderJson("msg","success");
		}else{
			renderJson("msg","设置失败，请重试！");
		}
	}
	
	//用户物理删除
	public void del(){
		boolean ret = false;
		if(StrKit.notBlank(getPara("id"))){
			ret = Db.deleteById("device_user",getPara("id"));
		}
		if(StrKit.notBlank(getPara("ids"))){
			ret = Db.update("delete from device_user where id in ("+getPara("ids")+")")>0;
		}
		if(ret){
			Db.deleteById("device_bind", "userid", getPara("id"));
			renderJson("msg","success");
		}
		else renderJson("msg","删除失败，请重试！");
	}	
	
	//使用统计
	public void userstatis(){
		Record user = Db.findById("device_user",getPara(0));
		String monthStr = DateUtil.monthStr();
		setAttr("currMonth",monthStr.substring(4));
		setAttr("duser",user);
		setAttr("plist",Db.find("select * from device_product where nid = ? order by intime desc",user.getInt("nid")));
		int year = Integer.parseInt(monthStr.substring(0, 4));
		setAttr("ylist",new int[]{year,year-1,year-2});
		render("userstatis.html");
	}

	public void statisyear(){
		String rekey = RedisConstants.USER_OPR_MONTH+getPara("userid");
		String year = getPara("year",DateUtil.yearStr());
		Map<?,?> map = Redis.use().hgetAll(rekey);
		List<Object> data = new ArrayList<>();
 		for(int i=1;i<=12;i++){
			String month = year+StringUtils.leftPad(Integer.toString(i),2,"0");
			if(map.get(month)==null){
				data.add(0);
			}
			else data.add(Integer.parseInt((String)map.get(month)));
		}	
 		Map<String, Object> res = new HashMap<String, Object>(); 
		res.put("msg", "success");
		res.put("data", data);
		res.put("name", year+"年");
		renderJson(res);
	}

	public void statismonth(){
		String rekey = RedisConstants.USER_OPR_DAY+getPara("userid");
		String month = getPara("month",DateUtil.monthStr());
		Map<?,?> map = Redis.use().hgetAll(rekey);
		List<Object> data = new ArrayList<>();
		List<Object> axis = new ArrayList<>();
 		for(int i=1;i<=DateUtil.monthLength();i++){
			String day = month+StringUtils.leftPad(Integer.toString(i),2,"0");
			axis.add(i);
			if(map.get(day)==null){
				data.add(0);
			}
			else data.add(Integer.parseInt((String)map.get(day)));
		}	
 		Map<String, Object> res = new HashMap<String, Object>(); 
		res.put("msg", "success");
		res.put("data", data);
		res.put("name", month.substring(0, 4)+"年"+ month.substring(4)+"月");
		res.put("axis", axis);
		renderJson(res);
	}
	//给用户发消息 需要用户在线
	public void msg(){
		
	}
}